﻿// Upgrade NOTE: upgraded instancing buffer 'Props' to new syntax.

Shader "Unlit/GpuSkinning"
{
	Properties
	{
		_MainTex ("Texture", 2D) = "white" {}
	}
	SubShader
	{
		Tags { "RenderType"="Opaque" }
		LOD 100

		Pass
		{
			CGPROGRAM
			#pragma vertex vert
			#pragma fragment frag
			// make fog work
			#pragma multi_compile_fog
			#pragma multi_compile_instancing
			
			#include "UnityCG.cginc"

			struct appdata
			{
				float4 vertex : POSITION;
				float2 uv : TEXCOORD0;
				float4 boneIndices: TEXCOORD1;
				float4 weight: TEXCOORD2;
				UNITY_VERTEX_INPUT_INSTANCE_ID
			};

			struct v2f
			{
				float2 uv : TEXCOORD0;
				UNITY_FOG_COORDS(1)
				float4 vertex : SV_POSITION;
			};

			sampler2D _MainTex;
			float4 _MainTex_ST;

			float4 BonePalette[64 * 3];
			UNITY_INSTANCING_BUFFER_START(Props)
				UNITY_DEFINE_INSTANCED_PROP(float4x4, _WorldTransform)
#define _WorldTransform_arr Props
			UNITY_INSTANCING_BUFFER_END(Props)

			v2f vert (appdata v)
			{
				UNITY_SETUP_INSTANCE_ID(v);

				v2f o;

				float4x4 combineWorldMat;
				combineWorldMat[0] = BonePalette[v.boneIndices.x * 3] * v.weight.x + BonePalette[v.boneIndices.y * 3] * v.weight.y + BonePalette[v.boneIndices.z * 3] * v.weight.z + BonePalette[v.boneIndices.w * 3] * v.weight.w;
				combineWorldMat[1] = BonePalette[v.boneIndices.x * 3 + 1] * v.weight.x + BonePalette[v.boneIndices.y * 3 + 1] * v.weight.y + BonePalette[v.boneIndices.z * 3 + 1] * v.weight.z + BonePalette[v.boneIndices.w * 3 + 1] * v.weight.w;
				combineWorldMat[2] = BonePalette[v.boneIndices.x * 3 + 2] * v.weight.x + BonePalette[v.boneIndices.y * 3 + 2] * v.weight.y + BonePalette[v.boneIndices.z * 3 + 2] * v.weight.z + BonePalette[v.boneIndices.w * 3 + 2] * v.weight.w;
				combineWorldMat[3] = float4(0, 0, 0, 1);
				o.vertex = mul(combineWorldMat, v.vertex);
				o.vertex = mul(UNITY_ACCESS_INSTANCED_PROP(_WorldTransform_arr, _WorldTransform), o.vertex);
				o.vertex = mul(UNITY_MATRIX_VP, o.vertex);
				o.uv = TRANSFORM_TEX(v.uv, _MainTex);
				UNITY_TRANSFER_FOG(o,o.vertex);
				return o;
			}

			
			fixed4 frag (v2f i) : SV_Target
			{
				// sample the texture
				fixed4 col = tex2D(_MainTex, i.uv);
				// apply fog
				UNITY_APPLY_FOG(i.fogCoord, col);
				return col;
			}
			ENDCG
		}
	}
}
